﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;
namespace E02
{
    /// <summary>
    /// 采购订货单
    /// </summary>
    public class A03 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A03";
                List<E02A03M> ht = new List<E02A03M>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " select a.FID BillId,a.FBILLNO Number,case when FDate = '' then '1990-01-01 00:00:00' when FDate IS null then '1990-01-01 00:00:00' else FDate end OrderDate,(case b.FText when '' then null else b.FText end) PurchItem,(case e.F_102 when '' then null else e.F_102 end) Material,a.FDecimal Num,FDecimal10 BuyNum,d.FName BalWay, " +
                                "FDecimal1 Price,FDecimal12 BuyMoney,FDecimal2 TaxRate,FDecimal5 TranPrice,FDecimal6 TranTaxRate,FDecimal3 TaxPrice,FDecimal4 TaxMoney, " +
                                "FDecimal9 TotalMoney,FDecimal8 TranMoney,a.FNOTE,c.FName MeasureWay,FDecimal11 BalanceNum,FComboBox1 BuyWay,FDecimal13 OtherPrice, " +
                                "FDecimal14 OtherMoney,FMultiCheckStatus,FComboBox ControlWay,FDecimal7 TranPriceWithTax,(case f.F_102 when '' then null else f.F_102 end) SupplierID,FCheckBox IsForbid, " +
                                "FCheckBox1 IsForbidBalance,a.FNote Note,a.FBase6 NeedCheck,g.FName Biller,h.FName Checker " +
                                ",(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID from KEW_PurchOrderQty  a " +
                                "left join KEW_PURCHITEMENTRY b on a.FBASE4 = b.FID  left join KEW_PurchConfig c on a.FBASE5 = c.FID " +
                                "left join t_Settle d on a.FBASE9 = d.FItemID " +
                                "left join t_ICItem e on e.FItemID = a.FBASE1 " +
                                "left join t_Supplier f on f.FItemID = a.FBASE " +
                                "left join t_User g on g.FUserID = a.FBiller and a.FBiller <> 0  " +
                                "left join t_User h on h.FUserID = a.FUser and a.FUser <> 0 where a.FID = @FID";
                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        if (row["SupplierID"] == null || row["SupplierID"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("采购订货单数据提取错误:{0}", "请先配置基础资料供应商对应关系！编号：" + row["Number"].ToString());
                            return;
                        }
                        if (row["PurchItem"] == null || row["PurchItem"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("采购订货单数据提取错误:{0}", "请先配置基础资料大宗原料对应关系！编号：" + row["Number"].ToString());
                            return;
                        }
                        if (row["Material"] == null || row["Material"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("采购订货单数据提取错误:{0}", "请先配置基础资料物料对应关系！编号：" + row["Number"].ToString());
                            return;
                        }
                        E02A03M e02A03M = new E02A03M();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++)
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A03M, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        ht.Add(e02A03M);
                        result.Content = handler.Json.ClassToJson(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("采购订货单数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("采购订货单提取错误:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 采购订货单
    /// </summary>
    public class E02A03M
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 单据编号
        /// </summary>
        public String Number;
        public String SupplierID = String.Empty;
        public DateTime OrderDate ; //下单日期
        public String PurchItem;                // 大宗原料
        public String Material = String.Empty; //物料
        public Decimal Num = 0; //数量
        public Decimal BuyNum = 0; //购进数量
        public Decimal Price = 0; //单价
        public Decimal BuyMoney = 0; //购进金额
        public Decimal TaxRate = 0; //税率
        public Decimal TranPrice = 0; //运输单价
        public Decimal TranTaxRate = 0; //运费税率
        public Decimal TaxPrice = 0; //含税单价
        public Decimal TaxMoney = 0; //含税金额
        public Decimal TotalMoney = 0; //总金额
        public Decimal TranMoney = 0; //运输金额
        public String FNOTE = String.Empty; //备注
        public String MeasureWay = String.Empty; //计量方案
        public String BalWay = String.Empty; //结算方式
        public Decimal BalanceNum = 0; //余量
        public String BuyWay = String.Empty; //采购方式
        public Decimal OtherPrice = 0; //其他单价
        public Decimal OtherMoney = 0; //其他金额

        public String FMultiCheckStatus = String.Empty; //审核流程状态
        public String ControlWay = String.Empty; //管控方式

        public Decimal TranPriceWithTax;    //含税运价
        public bool IsForbid;        // 禁用
        public bool IsForbidBalance;  // 禁止结算
        public String Note;         // 备注
        public int NeedCheck;       // 是否需要质检
        public String Biller = String.Empty;
        public String Checker = String.Empty;

        /// <summary>
        /// EAS财务组织
        /// </summary>
        public String FiUnitID;
    }
}
